-- UIShowTip
-- Create by cheny3
-- 提示界面

--UIShowTip继承自Layer
UIShowTip = class("UIShowTip", function()
    return cc.Layer:create();
end);


function UIShowTip.create(title, content, extraBtnInfo)
    return UIShowTip.new(title, content, extraBtnInfo);
end

-- 内部函数声明
local resize;

-- 构造函数
function UIShowTip:ctor(title, content, extraBtnInfo)
    -- 初始化
    local node = cc.CSLoader:createNode("layout/dialog/ShowTip.csb");
    self:addChild(node);
    self:setName("UIShowTip");

    self:setLocalZOrder(UIMgr.BONUS_NOTIFY_ZORDER + 1);

    self.node = node;
    self.title = title;
    self.content = content;
    self.extraBtnInfo = extraBtnInfo or {};

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    resize(node);
end

-- 重绘
function UIShowTip:redraw()
    local titleLabel = findChildByName(self.node, "CT/title");
    local contentLabel = findChildByName(self.node, "CT/content");
    local bg = findChildByName(self.node, "CT/image_bg")

    TextStyleM.setTextStyle(titleLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_DARK_RED);
    TextStyleM.setTextStyle(contentLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, nil, nil, nil, nil, nil, TextStyleM.TEXT_SIZE_SMALL);

    titleLabel = applyString(titleLabel, self.title);

    contentLabel = applyString(contentLabel, self.content);

    AlignM.adaptLayout(bg, AlignM.BG_SIZE_NORMAL, { titleLabel, contentLabel});

    for i = 1, 4 do
        local child = findChildByName(bg, "bg" .. i);
        if child then
            child:setTouchEnabled(false);
        end
    end
    contentLabel:setTouchEnabled(false);
    titleLabel:setTouchEnabled(false);


end

-- 注册点击事件
function UIShowTip:registerTouchEvent()
    local bgNode = findChildByName(self.node,"bg/bgImg");
    bgNode:setTouchEnabled(true);
    local function onBgClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 点击关闭
            UIMgr.getCurrentScene():removeFormByName("UIShowTip");
        end
    end
    AddBackEventHandler(self, bgNode, onBgClick);

    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIShowTip");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 额外信息按钮
    local btnExtra = findChildByName(self.node, "BT/btn_extra");
    if type(self.extraBtnInfo) == "table" and
        type(self.extraBtnInfo["btn_text"]) == "string" and
        self.extraBtnInfo.callback then
        btnExtra:setVisible(true);

        local function onExtraClick(sender, eventType)
            if eventType == ccui.TouchEventType.ended then
                if type(self.extraBtnInfo["callback"]) == "function" then
                    AudioM.playFx("button_return");

                    -- 调用传入的回调函数
                    self.extraBtnInfo.callback();
                    UIMgr.getCurrentScene():removeFormByName("UIShowTip");
                end
            end
        end
        btnExtra:addTouchEventListener(onExtraClick);
        TextStyleM.setTextStyle(btnExtra, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
        btnExtra:setTitleText(self.extraBtnInfo["btn_text"]);
    else
        btnExtra:setVisible(false);
    end
end

-- 适配
function resize(node)
    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end